package dao;

import java.util.Vector;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;

public class Bird {
        /* The columns in the table that correspond to the attributes of a Bird
         * object. Used when building SQL queries.
         */
        private static final String staticFields =
                "Bird.scientific_name, " +
                "Bird.size, " +
                "Bird.risk_of_extinction, " +
                "Bird.weight, " +
                "Bird.general_information, " +
                "Bird.map";

        private String scientificName;
        private float size;
        private float riskOfExtinction;
        private float weight;
        private String generalInformation;
        private String map;

        public Bird(String scientificName, float size, float riskOfExtinction,
                    float weight, String generalInformation, String map)
        {
                this.scientificName = scientificName;
                this.size = size;
                this.riskOfExtinction = riskOfExtinction;
                this.weight = weight;
                this.generalInformation = generalInformation;
                this.map = map;
        }

        /* Getters / setters */
        public String getMap()
        {
                return map;
        }

        public String getGeneralInformation()
        {
                return generalInformation;
        }

        public float getWeight()
        {
                return weight;
        }

        public float getRiskOfExtinction()
        {
                return riskOfExtinction;
        }

        public float getSize()
        {
                return size;
        }

        public String getScientificName()
        {
                return scientificName;
        }

        public void setScientificName(String scientificName)
        {
                this.scientificName = scientificName;
        }

        public void setSize(float size)
        {
                this.size = size;
        }

        public void setWeight(float weight)
        {
                this.weight = weight;
        }

        public void setGeneralInformation(String generalInformation)
        {
                this.generalInformation = generalInformation;
        }

        public void setMap(String map)
        {
                this.map = map;
        }

        /* Queries */
        public static Vector<Bird> byFood(String food)
        throws SQLException
        {
                String SQLQuery;

                SQLQuery = "SELECT " + staticFields +
                           " FROM Bird, Food " +
                           "WHERE " +
                           "Food.Bird_scientific_name = Bird.scientific_name " +
                           "AND Food.type_of_food = '" + food + "'";

                return runSQLQuery(SQLQuery);
        }

        public static Vector<Bird> bySize(float size)
        throws SQLException
        {
                return runSQLQuery("SELECT " + staticFields + " " +
                                   "FROM Bird " +
                                   "WHERE Bird.size = " + size);
        }

        public static Vector<Bird> byPopularName(String popularName)
        throws SQLException
        {
                return runSQLQuery("SELECT " + staticFields +
                                   " FROM Bird, Popular_Names " +
                                   "WHERE " +
                                   "Popular_Names.Bird_scientific_name = " +
                                   "Bird.scientific_name " +
                                   "AND Popular_Names.name = '" + popularName + "'");
        }

        public static Vector<Bird> runSQLQuery(String SQLQuery)
        throws SQLException
        {
                ResultSet result;
                Statement statement;
                Vector<Bird> birds = new Vector<Bird>();

                statement = ConnectionManager.getStatement();
                result = statement.executeQuery(SQLQuery);

                while (result.next()) {
                        Bird b;

                        b = new Bird(result.getString("scientific_name"),
                                     result.getFloat("size"),
                                     result.getFloat("risk_of_extinction"),
                                     result.getFloat("weight"),
                                     result.getString("general_information"),
                                     result.getString("map"));

                        birds.add(b);
                }

                return birds;
        }
}
